import React, { useEffect, useState } from 'react';
import { getArticleList ,getDelete} from "../../store/counterSlice";
import { useSelector, useDispatch } from 'react-redux'
import { Table ,Space} from "antd";

import instance from "../../apis/request";
function Article() {
    const {articledata,count} = useSelector((state) => state.counter)
  const dispatch = useDispatch()
  const [data,setData]=useState({
    page:1,//页码  必传
pageSize:6,//每页显示条数 必传
keyword:null,//搜索标题
tag:null//搜索标签

  })
  const [fy,setFy]=useState({
    current:1,
    pageSize:6,
    total:0
  })
  const aa=async()=>{
    await instance.get('/article/list',{
        params:data
    }).then(res=>{
        console.log(res);
        
        dispatch(getArticleList(res))
        setFy({
            ...fy,
            total:res.count
        })
    })
  }
  useEffect(()=>{
    aa()
  },[data,dispatch])
  const dd=(diss)=>{
    setData({
        ...data,
        page:diss.current,
        pageSize:diss.pageSize
    }),
    setFy({
        ...fy,
        current:diss.current,
        pageSize:diss.pageSize,
        total:diss.total

    })
  }

  const del=(record)=>{
    if(confirm('确定删除吗')){
      dispatch(getDelete(record.id))
    }
    
  }
  const columns = [
  {
    title: '编号',
    dataIndex: 'id',
    key: 'id',
  },
  {
    title: '标题',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: '介绍',
    dataIndex: 'content',
    key: 'content',
  },
  {
    title: 'Action',
    key: 'action',
    render: (_, record) => (
      <Space size="middle">
        <a onClick={()=>del(record)}>删除</a>
        <a>编辑</a>
      </Space>
    ),
  },
];
    return (
        <div>
            <Table dataSource={articledata} columns={columns} pagination={fy} onChange={dd} />
        </div>
    );
}

export default Article;


